from django.contrib import admin
from .models import Game, GameRound, PlayerAction


class GameRoundInline(admin.TabularInline):
    model = GameRound
    extra = 0
    readonly_fields = ('created_at',)


class PlayerActionInline(admin.TabularInline):
    model = PlayerAction
    extra = 0
    readonly_fields = ('created_at',)


@admin.register(Game)
class GameAdmin(admin.ModelAdmin):
    """游戏管理"""
    list_display = ('id', 'player1', 'player2', 'status', 'current_turn', 'winner', 'created_at')
    list_filter = ('status', 'created_at')
    search_fields = ('player1__username', 'player1__nickname', 'player2__username', 'player2__nickname')
    ordering = ('-created_at',)
    inlines = [GameRoundInline]


@admin.register(GameRound)
class GameRoundAdmin(admin.ModelAdmin):
    """游戏回合管理"""
    list_display = ('game', 'round_number', 'player1_card', 'player2_card', 'winner', 'is_finished', 'created_at')
    list_filter = ('is_finished', 'created_at')
    search_fields = ('game__id', 'player1_card__name', 'player2_card__name')
    ordering = ('-created_at',)


@admin.register(PlayerAction)
class PlayerActionAdmin(admin.ModelAdmin):
    """玩家行动管理"""
    list_display = ('game', 'round', 'player', 'action_type', 'card', 'created_at')
    list_filter = ('action_type', 'created_at')
    search_fields = ('player__username', 'player__nickname', 'card__name')
    ordering = ('-created_at',)
